<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>首页</title>
    <link
      href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css"
      rel="stylesheet"
    />
    <!-- <link
      href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.2/css/bootstrap.min.css"
      rel="stylesheet"
    /> -->
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="Pagination.js"></script>
    <style>
      img {
        width: 80px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <a href="/register" class="btn btn-info">去注册</a>
      <a href="/login" class="btn btn-warning">去登录</a>
      <a href="/myshop" class="btn btn-success">个人中心</a>
      <a href="/shopcar" class="btn btn-danger">购物车</a>
      <table class="table table-bordered table-hover">
        <thead>
          <tr>
            <th>图片</th>
            <th>商品名</th>
            <th>价格</th>
            <th>描述</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody id="tb">
          <!-- <tr>
            <td>1</td>
            <td>1</td>
            <td>1</td>
            <td>1</td>
            <td>
              <button class="btn btn-danger">加入购物车</button>
            </td>
          </tr> -->
        </tbody>
      </table>
      <div class="myPagination"></div>
    </div>
    <script>
      // $(".myPagination").Pagination({
      //   page: 1,
      //   count: 100,
      //   groups: 10,
      //   onPageChange: function (page) {
      //     console.log("当前是:" + page);
      //   },
      // });
      //调用接口，将用户列表渲染到页面上
      onloadUserList(1, 5); //1是当前为第几页，5为当页多少条数据
      function onloadUserList(page, limit = 5) {
        // ajax的两种传参方式，传到服务端进行接收
        // url: `/userList?page=${page}&limit=${limit}`,
        // data: {
        //     page: page, //当前是第几页
        //     limit: limit, //代表每页几条内容
        //   },
        $.ajax({
          url: "/shopList",
          type: "get",
          data: {
            page: page, //当前是第几页
            limit: limit, //代表每页几条内容
          },
          success: function (res) {
            console.log(res);
            let str = "";
            res.data.forEach((item, index) => {
              str += `<tr>
                <td><img src="${item.pimg}"></td>
                <td>${item.pname}</td>
                <td>${item.pprice}</td>
                <td>${item.pdesc}</td>
                <td><a class="btn btn-primary" href="/shopdetail?pid=${item._id}">商品详情</a></td>
                </tr>`;
            });
            $("#tb").html(str);
            // 分页
            $(".myPagination").Pagination({
              page: page, //当前页码
              count: res.total, //总数量
              limit: limit, //每一页的数量
              // groups: 2, //按钮显示个数
              onPageChange: function (page) {
                console.log("当前是:" + page);
                //获取到一页数据后，再次调用函数，根据当前获取下次页面的数据
                onloadUserList(page, limit);
              },
            });
          },
        });
      }
    </script>
  </body>
</html>
